package com.example.recyclerview;

import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView()
    {
        RecyclerView recyclerView = findViewById(R.id.rec);

        LinearLayoutManager llm = new LinearLayoutManager(this);
        //LinearLayoutManager默认情况下是垂直即Vertical ListView
//        llm.setOrientation(LinearLayoutManager.VERTICAL);
        //水平列表显示 HorizontalListView
//        llm.setOrientation(LinearLayoutManager.HORIZONTAL);

        //GridView
        //网格模式，默认垂直，给定默认为列
        //网格模式，设置水平后，原本的列数变为行数，水平滚动
        //参数4：排列上下反转
        GridLayoutManager glm = new GridLayoutManager(this, 4,
                GridLayoutManager.VERTICAL, false);

        //RecyclerView一开始必须给定列表样式，否则列表无法显示
        recyclerView.setLayoutManager(glm);

        List<ContactModel> data = new ArrayList<>();

        for (int i = 0; i < 20; i++)
        {
            ContactModel model = new ContactModel();

            model.setContactName("张三 " + i);
            model.setContactIconResId(R.mipmap.ic_launcher);

            data.add(model);
        }

        MyAdapter adapter = new MyAdapter(this);
        adapter.setData(data);

        recyclerView.setAdapter(adapter);
    }
}